﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.IO;
using System.Windows.Media;
using System.Windows.Shapes;

namespace TouchUmlUI
{
    public class AktorObj : AssociableObj
    {

        static BitmapImage src;
        private Image img;
        private TextBlock tb;
        public Rectangle r = new Rectangle();

        public override void Refresh()
        {
            tb.Text = guml.obj_uml.Nazwa;
            guml.panel.UpdateLayout();
            tb.UpdateLayout();
            updateDependencyProperties();
        }


        public override void showHints(bool a_show)
        {
            int thick = a_show ? 1 : 0;
            r.StrokeThickness = thick;
        }

        static AktorObj()
        {
            src = new BitmapImage();
            src.BeginInit();
            src.UriSource = new Uri(Directory.GetCurrentDirectory() + "\\gfx\\aktor.png");
            src.EndInit();            
        }

        public override void updateDependencyProperties()
        {
            base.updateDependencyProperties();
            double lx = (54 - tb.ActualWidth) / 2.0f;
            double ly = 135;
            Canvas.SetLeft(tb, lx);
            Canvas.SetTop(tb, ly);
            Canvas.SetLeft(r, lx - 5);
            Canvas.SetTop(r, ly - 5);
            r.Width = tb.ActualWidth + 10;
            r.Height = tb.ActualHeight + 10;
        }

        public AktorObj(String a_title, int a_width, int a_height, int a_left, int a_top, OurCanvas a_canvas, int a_id, GUML guml) 
            : base(guml)
        {

            img = new Image();            
            img.Width = 54;
            img.Height = 132;
            img.Source = src;
            img.Stretch = System.Windows.Media.Stretch.Fill;

            guml.canvas = a_canvas;

            if (a_width < 0) { a_left = a_left + a_width; a_width = -a_width; }
            if (a_height < 0) { a_top = a_top + a_height; a_height = -a_height; }

            x = a_left;
            y = a_top;
            width = a_width;
            height = a_height;

            Canvas bg = new Canvas();
            bg.Width = 54;
            bg.Height = 132;
            bg.Background = Brushes.White;
            bg.Opacity = 0.01f;

            guml.panel = new Canvas();
            guml.panel.Width = 54;
            guml.panel.Height = 132;
            guml.panel.Children.Add(img);
            InkCanvas.SetLeft(guml.panel, x);
            InkCanvas.SetTop(guml.panel, y);

            r.Stroke = Brushes.Black;
            r.StrokeThickness = 1;
            r.StrokeDashArray.Add(5);
            r.Fill = new SolidColorBrush(Color.FromArgb(10, 255, 255, 255));
            r.MouseDown += tb_MouseDown;
            r.Opacity = 0.5;

            tb = new TextBlock();            
            tb.MouseDown += new System.Windows.Input.MouseButtonEventHandler(tb_MouseDown);
            tb.SizeChanged += new System.Windows.SizeChangedEventHandler(tb_SizeChanged);
            guml.panel.Children.Add(r);
            guml.panel.Children.Add(tb);

            guml.canvas.Children.Add(guml.panel);
            guml.panel.UpdateLayout();            
            this.updateDependencyProperties();
        }

        void tb_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
        {
            double lx = (54 - tb.ActualWidth) / 2.0f;
            double ly = 135;
            Canvas.SetLeft(tb, lx);
            Canvas.SetTop(tb, ly);
            Canvas.SetLeft(r, lx - 5);
            Canvas.SetTop(r, ly - 5);
            r.Width = tb.ActualWidth + 10;
            r.Height = tb.ActualHeight + 10;
        }

        void tb_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (SignalR.windowParent.handWrite != null) return;
            SignalR.windowParent.handWrite = new UCUTextRecognizer.HandWrite();
            UCUTextRecognizer.HandWrite handWrite = SignalR.windowParent.handWrite;
            handWrite.InitializeComponent();


            try
            {
                if (!guml.obj_uml.Nazwa.StartsWith("Aktor"))
                {
                    handWrite.setExistingText(guml.obj_uml.Nazwa);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            double sy = getY() + 90;            
            double nh = 180;
            double sx = SignalR.windowParent.sv.HorizontalOffset + 50;
            double nw = SignalR.windowParent.sv.ActualWidth - 100;                        

            handWrite.Width = nw;
            handWrite.Height = nh;            
            InkCanvas.SetLeft(handWrite, sx);
            InkCanvas.SetTop(handWrite, sy);
            handWrite.Opacity = 1.0f;
            //
            handWrite.bindWithObject(guml.obj_uml, guml.canvas, guml.umlobj);
            SignalR.windowParent.active_canvas.Children.Add(handWrite);
            e.Handled = true;
        }

        public override void setX(int ax)
        {
            InkCanvas.SetLeft(guml.panel, ax);
        }

        public override void setY(int ay)
        {
            InkCanvas.SetTop(guml.panel, ay);
        }


    }
}
